layui.use(['form', 'jquery'], function () {

    const form = layui.form,
        $ = layui.jquery;

    const id = sessionStorage.getItem("dailyDetailsId");
    findById(id);

    /**
     * 查询单个
     * @param id
     */
    function findById(id) {
        let result = commonAjax('/daily/findById', {id: id}, 'get');
        setData(result.data);
    }

    /**
     * 赋值
     */
    function setData(data) {
        $('#id').val(data.id);
        $('#userCode').val(data.userCode);
        $('#username').val(data.username);
        $('#title').val(data.title);
        $('#dailyTime').val(data.dailyTime);
        $('#headMaster').val(data.headMaster);
        $('#coach').val(data.coach);
        $('#studyContent').val(data.studyContent);
        $('#harvest').val(data.harvest);
        $('#shortage').val(data.shortage);
        form.render();
    }


    /**
     * 保存
     */
    form.on('submit(save)', function (data) {
        let formData = JSON.stringify(data.field);
        let result = commonAjax('/daily/save', formData, 'post');
        if (result.code === 200) {
            layerMsgSaveSuccess();
        } else {
            layerMsgSaveFail();
        }

        return false;
    });

    /**
     * 提交
     */
    form.on('submit(commit)', function (data) {
        let formData = JSON.stringify(data.field);

        let result = commonAjax('/daily/add', formData, 'post');
        if (result.code === 200) {

            layerMsgAddSuccess();
        } else {
            layerMsgAddFail();
        }

        return false;
    });

});